#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************************
#   > File Name : install_sricpt.py
#   > Author : chengzb
#   > Mail : 
#   > Created Time : 2015-08-27
#   > Comment : 
# *************************************************************************
import os

def instll_content() : 
    cont = '\
#! /usr/bin/bash\n\
\n\
#在Deb解包之前，将会运行该脚本\n\
pre_install()\n\
{\n\
  :\n\
} \n\
#负责完成安装包时的配置\n\
post_install()\n\
{\n\
  :\n\
} \n\
#负责停止与软件包相关联的daemon服务\n\
pre_remove()\n\
{\n\
  :\n\
} \n\
#负责修改软件包连接或文件关联\n\
post_remove()\n\
{\n\
  :\n\
} \n\
pre_upgrade()\n\
{\n\
  :\n\
} \n\
post_upgrade()\n\
{\n\
  :\n\
} \n\
pre_downgrade()\n\
{\n\
   :\n\
} \n\
post_upgrade()\n\
{\n\
  :\n\
} \n\
    '
    
    return cont


'''
    @brief : create CPK dir's root dir
'''      

def install_generation(path,filename) : 
    write(path,filename)
    b = write_script(path,filename)
    return b


def write(path,filename) : 
    inst_path = path +"/CPK/"+filename+".install"
    print inst_path
    f = open(inst_path, "w")
    try :
        if f : 
            f.writelines(instll_content())
    finally : 
        f.close()


def write_script(path,filename):
    postinst_path = path + "/DEBIAN/postinst"
    preinst_path = path + "/DEBIAN/preinst"
    postrm_path = path + "/DEBIAN/postrm"
    prerm_path = path + "/DEBIAN/prerm"
    if os.path.exists(postinst_path):
        fileadd = open(postinst_path)
        inst_path = path + "/CPK/"+filename+".install"
        f = open(inst_path)
        content = f.read()
        try:
            contentadd = fileadd.readlines()
            for eachline in contentadd:
                eachline = "    " + eachline
                pos = content.find("} \n\
pre_remove()")
                if pos != -1:
                    content = content[:pos] + eachline + content[pos:]
                    f = open(inst_path, "w")
                    f.write(content)
                    f.close()
            #print "Write postinst OK" 
        finally:
            fileadd.close()
    if os.path.exists(preinst_path):
        fileadd = open(preinst_path)
        inst_path = path + "/CPK/"+filename+".install"
        f = open(inst_path)
        content = f.read()
        try:
            contentadd = fileadd.readlines()
            for eachline in contentadd:
                eachline = "    " + eachline
                pos = content.find("} \n\
post_install()")
                if pos != -1:
                    content = content[:pos] + eachline + content[pos:]
                    f = open(inst_path, "w")
                    f.write(content)
                    f.close()
            #print "Write preinst OK" 
        finally:
            fileadd.close()
    if os.path.exists(postrm_path):
        fileadd = open(postrm_path)
        inst_path = path + "/CPK/"+filename+".install"
        f = open(inst_path)
        content = f.read()
        try:
            contentadd = fileadd.readlines()
            for eachline in contentadd:
                eachline = "    " + eachline
                pos = content.find("} \n\
pre_upgrade()")
                if pos != -1:
                    content = content[:pos] + eachline + content[pos:]
                    f = open(inst_path, "w")
                    f.write(content)
                    f.close()
            #print "Write postrm OK" 
        finally:
            fileadd.close()
    if os.path.exists(prerm_path):
        fileadd = open(prerm_path)
        inst_path = path + "/CPK/"+filename+".install"
        f = open(inst_path)
        content = f.read()
        try:
            contentadd = fileadd.readlines()
            for eachline in contentadd:
                eachline = "    " + eachline
                pos = content.find("} \n\
post_remove()")
                if pos != -1:
                    content = content[:pos] + eachline + content[pos:]
                    f = open(inst_path, "w")
                    f.write(content)
                    f.close()
            #print "Write prerm OK" 
        finally:
            fileadd.close()
    #读取全部内容并返回
    inst_path = path + "/CPK/"+filename+".install"
    all_the_data = open(inst_path).read()
    return all_the_data

if __name__ == "__main__" :
    path = "/home/zr/下载/youdao-dict_1.1.0-0-deepin_amd64_cache/"
    filename = "sougou"
    install_generation(path,filename)


